package com.ceres.farm.controller;

import com.ceres.entity.PageResult;
import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import com.ceres.farm.pojo.CustFarm;
import com.ceres.farm.pojo.CustFarmland;
import com.ceres.farm.pojo.History;
import com.ceres.farm.service.FieldService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/field")
public class FieldController {
    @Autowired
    FieldService fieldService;
    /**
     * 添加大田
     * @param custFarmland
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody CustFarmland custFarmland){
        try {
            fieldService.add(custFarmland);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,StatusCode.ERROR);
        }
        return new Result(true, StatusCode.OK);
    }

    /**
     * 根据片区进行分页条件分页查询
     * @param pageSize
     * @param pageNumber
     * @param aid
     * @param keyword
     * @return
     */
    @GetMapping("/query")
    public Result query(String pageSize,String pageNumber,Integer aid,String keyword){
        Integer size=10;
        Integer number=1;
        if(StringUtils.isEmpty(pageSize)){
            try {
                size = Integer.parseInt(pageSize);
            } catch (NumberFormatException e) {

            }
        }
        if( StringUtils.isEmpty(pageNumber)){
            try {
                number = Integer.parseInt(pageNumber);
            } catch (NumberFormatException e) {

            }
        }

        try {
            Page<CustFarmland> pageList= fieldService.pageQuery(number,size,aid,keyword);
            PageResult pageResult=new PageResult(pageList.getTotal(),pageList.getResult());
            return new Result(true,StatusCode.OK,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

    /**
     * 根据大田ID查询大田详情
     * @param id
     * @return
     */
    @GetMapping("/queryDetailsById")
    public  Result queryById(Integer id){
        try {
            Map<String,Object> map= fieldService.queryById(id);
            return new Result(true,StatusCode.OK,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

    /**
     * 修改大田
     * @param custFarmland
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody CustFarmland custFarmland){
        try {
            fieldService.update(custFarmland);
            return new Result(true,StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }
    //查询大田历史信息
    @GetMapping("/findHistory")
    public Result findHistory(Integer fid){

        try {
            Map<String,Object> map= fieldService.queryById(fid);
            List<History> history= fieldService.findHistory(fid);
            map.put("history",history);
            return new Result(true,StatusCode.OK,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,StatusCode.ERROR);
        }
    }

}
